<?php

namespace app\common\traits;

use app\common\model\PushDevice;
use app\common\service\PushJiguangService;
use app\common\service\PushMobService;

/**
 * APP消息推送
 */
trait PushTrait
{
    /**
     * 极光通知推送
     * @param array $userIds
     * @param array $notification
     * @return false
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public static function notification4jiguang($userIds = [], $notification = [])
    {
        $flag = false;

        if( empty( $userIds ) || empty( $notification ) ) {
            return $flag;
        }

        //// user device
        $dWhere = [
            'user_id' => ['in', $userIds],
            'provider' => 0, //服务提供商, 0-极光推送, 1-mob推送;
        ];
        $deviceModel = new PushDevice();
        $registrationIds = $deviceModel->column($dWhere, 'registration_id');
        $registrationIds = array_unique( $registrationIds );

        if( empty( $registrationIds ) ) {
            return $flag;
        }

        $jgService = new PushJiguangService();
        $flag = $jgService->notificationPush($notification, $registrationIds);

        return $flag;
    }

    /**
     * mobpush通知推送
     * @param array $userIds
     * @param array $notification
     * @return bool|mixed|string
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public static function notification4mob($userIds = [], $notification = [])
    {
        $flag = false;

        if( empty( $userIds ) || empty( $notification ) ) {
            return $flag;
        }

        //// user device
        $dWhere = [
            'user_id' => ['in', $userIds],
            'provider' => 1, //服务提供商, 0-极光推送, 1-mob推送;
        ];
        $deviceModel = new PushDevice();
        $registrationIds = $deviceModel->column($dWhere, 'registration_id');
        $registrationIds = array_unique( $registrationIds );

        if( empty( $registrationIds ) ) {
            return $flag;
        }

        $mobService = new PushMobService();
        $flag = $mobService->notificationByRegId($notification, $registrationIds);

        return $flag;
    }
}